package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.discover.ChatNearbyInfo;
import com.zy.openchat.core.model.discover.ChatsNearbyInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.LocationHelper;

import java.util.HashMap;
import java.util.Map;


public class NearbyViewModel extends PagedViewModel<NearbyItemViewModel, ChatNearbyInfo> {
    public HashMap<Long, UserInfo> userInfoHashMap = new HashMap<>();
    private FragmentActivity activity;

    public NearbyViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    public void requestData(int page) {
        LocationHelper.getIntance().getLocation(activity, aMapLocation -> {
            if (aMapLocation != null) {
                TelegramManager.getTelegramManager().searchChatsNearby(getApplication(), aMapLocation, new TgResultBlock<ChatsNearbyInfo>() {
                    @Override
                    public void onResult(Map<String, Object> request, JsonObject response, ChatsNearbyInfo obj) {
                        finishRefreshAndLoad(page, obj == null ? null : obj.users_nearby, NearbyItemViewModel.class);
                    }

                    @Override
                    public void onTimeout(Map<String, Object> request) {
                        finishRefreshAndLoad(page, null, NearbyItemViewModel.class);
                    }
                });
            } else {
                finishRefreshAndLoad(page, null, NearbyItemViewModel.class);
            }

        });
    }

    @Override
    public int initContentView() {
        return R.layout.item_nearby;
    }

    public void setContext(FragmentActivity activity) {

        this.activity = activity;
    }
}
